class Subchannel:
    def __init__(self, id):
        self.id = id
        self.mass_flow_rate = 0.0  # 质量流量 (kg/s)
        self.momentum = 0.0         # 动量 (N·s)
        self.neighbors = []         # 相邻子通道

    def add_neighbor(self, neighbor):
        self.neighbors.append(neighbor)

    def update_flow(self, time_step):
        # 简单的质量和动量更新逻辑
        for neighbor in self.neighbors:
            # 假设质量流动到相邻子通道
            mass_transfer = 0.1 * (self.mass_flow_rate - neighbor.mass_flow_rate) * time_step
            self.mass_flow_rate -= mass_transfer
            neighbor.mass_flow_rate += mass_transfer
            
            # 更新动量
            self.momentum = self.mass_flow_rate * 1.0  # 假设速度为1 m/s
            neighbor.momentum = neighbor.mass_flow_rate * 1.0

def initialize_subchannels():
    # 创建子通道
    subchannels = {i: Subchannel(i) for i in range(1, 8)}
    
    # 定义相邻关系
    subchannels[1].add_neighbor(subchannels[2])
    subchannels[1].add_neighbor(subchannels[3])
    subchannels[2].add_neighbor(subchannels[1])
    subchannels[2].add_neighbor(subchannels[4])
    subchannels[3].add_neighbor(subchannels[1])
    subchannels[3].add_neighbor(subchannels[5])
    subchannels[4].add_neighbor(subchannels[2])
    subchannels[4].add_neighbor(subchannels[6])
    subchannels[5].add_neighbor(subchannels[3])
    subchannels[5].add_neighbor(subchannels[7])
    subchannels[6].add_neighbor(subchannels[4])
    subchannels[7].add_neighbor(subchannels[5])
    
    # 初始化质量流量
    subchannels[1].mass_flow_rate = 1.0  # 初始质量流量 (kg/s)
    subchannels[2].mass_flow_rate = 0.5
    subchannels[3].mass_flow_rate = 0.5
    subchannels[4].mass_flow_rate = 0.0
    subchannels[5].mass_flow_rate = 0.0
    subchannels[6].mass_flow_rate = 0.0
    subchannels[7].mass_flow_rate = 0.0

    return subchannels

def solve_flow(subchannels, time_step):
    for subchannel in subchannels.values():
        subchannel.update_flow(time_step)

def main():
    time_step = 0.01  # 时间步长 (s)
    subchannels = initialize_subchannels()
    
    # 进行流动求解
    for _ in range(100):  # 迭代次数
        solve_flow(subchannels, time_step)

    # 输出结果
    for subchannel in subchannels.values():
        print(f"Subchannel {subchannel.id}: Mass Flow Rate = {subchannel.mass_flow_rate:.2f} kg/s, Momentum = {subchannel.momentum:.2f} N·s")

if __name__ == "__main__":
    main()
